package com.yuexunit.finance.system.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.system.entity.LoansMoneyInfoEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @author wenwenlong
 */

@Implements(LoansMoneyInfoService.class)
public class LoansMoneyInfoServiceImpl implements LoansMoneyInfoService {

	@Override
	@Transactional
	public Long createLoansMoney(LoansMoneyInfoEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getLoansMoneyId();
	}

	@Override
	@Transactional
	public void updateLoansMoney(LoansMoneyInfoEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public LoansMoneyInfoEntity inquireLoansMoneyDetail(Long loansMoneyId) {
		return PersistUtil.get(LoansMoneyInfoEntity.class, loansMoneyId);
	}

	@Override
	public PageList<LoansMoneyInfoEntity> inquireLoansMoneyPageList(List<Long> lendUnitId, Date startDate, Date endDate, String financialBatch, String financingBatch, Boolean confirmState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(LoansMoneyInfoEntity.class);
		if (lendUnitId != null && lendUnitId.size() > 0) {
			criteria.add(Restrictions.in("lendUnitId", lendUnitId));
		}
		if (startDate != null) {
			criteria.add(Restrictions.ge("borrowedDate", startDate));
		}
		if (endDate != null) {
			criteria.add(Restrictions.le("borrowedDate", endDate));
		}
		if (confirmState != null) {
			criteria.add(Restrictions.eq("confirmState", confirmState));
		}
		if (StringUtil.isNotBlank(financialBatch)) {
			criteria.add(Restrictions.like("financialBatch", "%" + financialBatch + "%"));
		}
		if (StringUtil.isNotBlank(financingBatch)) {
			criteria.add(Restrictions.like("financingBatch", "%" + financingBatch + "%"));
		}
		criteria.add(Restrictions.ge("effectiveDate", DateUtils.now()));
		criteria.addOrder(Order.desc("createDateTime"));
		return PersistUtil.get(criteria, page);
	}

	@Override
	@Transactional
	public void deleteMoneyDetail(Long loansMoneyId) {
		PersistUtil.delete(LoansMoneyInfoEntity.class, loansMoneyId);
	}
}
